package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.TeamRole;
import cn.cdeden.knowledge.domain.dto.ProRoleToTeamRole;
import cn.cdeden.knowledge.domain.dto.TeamMemberQueryParam;
import cn.cdeden.knowledge.domain.dto.TeamRoleDto;
import cn.cdeden.knowledge.domain.dto.TeamRoleQueryParam;
import cn.cdeden.knowledge.enums.TeamParamEnum;
import cn.cdeden.knowledge.mapper.TeamRoleMapper;
import cn.cdeden.knowledge.service.TeamRoleService;
import cn.cdeden.knowledge.utils.PageUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Chen
 * @date 2022-11-22
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamRoleServiceImpl extends ServiceImpl<TeamRoleMapper, TeamRole> implements TeamRoleService {

    private final TeamRoleMapper teamRoleMapper;

    @Override
    public TableDataInfo<TeamRoleDto> queryAll(TeamRoleQueryParam query, Pageable pageable) {

        IPage<TeamRole> queryPage = PageUtil.toMybatisPage(pageable);
        QueryWrapper<TeamRole> predicate = QueryHelpMybatisPlus.getPredicate(query);
        IPage<TeamRole> page = teamRoleMapper.selectPage(queryPage, predicate);

        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page),TeamRoleDto.class);
//        return ConvertUtil.convertPage(page, TeamRoleDto.class,pageable);
    }

    @Override
    public List<TeamRoleDto> queryAll(TeamRoleQueryParam query) {
        QueryWrapper<TeamRole> predicate = QueryHelpMybatisPlus.getPredicate(query);
        List<TeamRoleDto> teamRoleDtos = MapstructUtils.convert(teamRoleMapper.selectList(predicate), TeamRoleDto.class);
        return teamRoleDtos;
    }

    @Override
    public TeamRole getById(Integer id) {
        return teamRoleMapper.selectById(id);
    }

    @Override
    public List<TeamRoleDto> getSupremeAuthority(String teamId) {
        return teamRoleMapper.getSupremeAuthority(teamId);
    }

    @Override
    public List<TeamRoleDto> getNextToOwner(String teamId) {
        return teamRoleMapper.getNextToOwner(teamId);
    }

    public int removeByTreeNodeId(String id) {
        HashMap map = new HashMap();
        map.put("tree_node_id", id);
        return teamRoleMapper.deleteByMap(map);
    }

    @Override
    public Long addDefaultRole(String teamId) {

        //添加所有者
        TeamRole teamRole = addOwnerRole(teamId);
        List<String> params = JSONArray.parseArray(teamRole.getParams(),String.class);
        Long id = teamRole.getId();

        teamRole.setRoleName("管理员");
        teamRole.setRank(2);
        teamRole.setRemark("可以新建、编辑、删除、下载、复制、移动文件和文件夹、分享连接、控制权限、分发同事、操作回收站、使用标签、控制模板、打印文件以及提交企业库");
        teamRole.setId(null);
        teamRole.setParams(JSONArray.toJSONString(params));
        teamRole.setIconColor("#fbb726");
        teamRoleMapper.insert(teamRole);

        teamRole.setRoleName("普通用户");
        teamRole.setRank(3);
        teamRole.setRemark("可以新建、编辑、删除、下载、复制、移动文件和文件夹、分享连接、分发同事、使用标签、控制模板、打印文件以及提交企业库");
        params = new ArrayList<>();
        setDefaultParams(params);
        params.add(TeamParamEnum.PRINT.getParam());
        params.add(TeamParamEnum.DISTRIBUTE.getParam());
        params.add(TeamParamEnum.LABEL.getParam());
        params.add(TeamParamEnum.COMMIT_ENTERPRISE.getParam());
        teamRole.setIconColor("#26cd8b");
        teamRole.setParams(JSONArray.toJSONString(params));
        teamRole.setId(null);
        teamRoleMapper.insert(teamRole);

        teamRole.setRoleName("只读用户");
        params = new ArrayList<>();
        params.add(TeamParamEnum.CHECK.getParam());
        teamRole.setParams(JSONArray.toJSONString(params));
        teamRole.setRank(4);
        teamRole.setRemark("可以查看文档及文件夹");
        teamRole.setId(null);
        teamRole.setIconColor("#cdcdcd");
        teamRoleMapper.insert(teamRole);

        return id;
    }

    private void setDefaultParams(List<String> params) {
        params.add(TeamParamEnum.ADD.getParam());
        params.add(TeamParamEnum.UPDATE.getParam());
        params.add(TeamParamEnum.DELETE.getParam());
        params.add(TeamParamEnum.CHECK.getParam());
        params.add(TeamParamEnum.DOWNLOAD.getParam());
        params.add(TeamParamEnum.COPY.getParam());
        params.add(TeamParamEnum.MOVE.getParam());
        params.add(TeamParamEnum.SHARE.getParam());
        params.add(TeamParamEnum.TEMPLATE.getParam());
    }

    @Override
    public TeamRole addOwnerRole(String teamId) {
        //添加所有者
        TeamRole teamRole = new TeamRole();
        List<String> params = new ArrayList<>();
        teamRole.setRoleName("所有者");
        teamRole.setRank(1);
        teamRole.setTeamId(teamId);
        setDefaultParams(params);
        params.add(TeamParamEnum.JURISDICTION.getParam());
        params.add(TeamParamEnum.DISTRIBUTE.getParam());
        params.add(TeamParamEnum.RECYCLE.getParam());
        params.add(TeamParamEnum.LABEL.getParam());
        params.add(TeamParamEnum.TEMPLATE.getParam());
        params.add(TeamParamEnum.PRINT.getParam());
        params.add(TeamParamEnum.COMMIT_ENTERPRISE.getParam());
        teamRole.setIconColor("#3385ff");
        teamRole.setParams(JSONArray.toJSONString(params));
        teamRole.setRemark("可以新建、编辑、删除、下载、复制、移动文件和文件夹，分享连接、控制权限、分发同事、操作回收站、使用标签、控制模板、打印文件、提交企业库以及删除团队");
        teamRoleMapper.insert(teamRole);
        return teamRole;
    }

    @Override
    public TeamRole addRole(ProRoleToTeamRole proRoleToTeamRole, String teamId) {
        TeamRole teamRole = new TeamRole();
        teamRole.setRoleName(proRoleToTeamRole.getRoleName());
        teamRole.setRank(proRoleToTeamRole.getRank());
        teamRole.setTeamId(teamId);
        teamRole.setIcon(proRoleToTeamRole.getIcon());
        teamRole.setIconColor(proRoleToTeamRole.getIconColor());
        teamRole.setParams(proRoleToTeamRole.getParams());
        teamRole.setRemark(proRoleToTeamRole.getRemark());
        teamRoleMapper.insert(teamRole);
        return teamRole;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TeamRole insert(TeamRoleDto resources) {
        TeamRole entity = MapstructUtils.convert(resources, TeamRole.class);
        boolean save = this.save(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(TeamRoleDto resources) {
        TeamRole entity = MapstructUtils.convert(resources, TeamRole.class);
        int ret = teamRoleMapper.updateById(entity);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids) {
        return teamRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateRole(TeamRoleDto teamRoleDto) {
        TeamRole entity = MapstructUtils.convert(teamRoleDto, TeamRole.class);
        teamRoleMapper.updateById(entity);
    }

    @Override
    public List<TeamRoleDto> getRole(String teamId) {
        QueryWrapper<TeamRole> predicate = QueryHelpMybatisPlus.getPredicate(new TeamMemberQueryParam());
        predicate.eq("team_id",teamId);
        predicate.orderByAsc("`rank`");
        return MapstructUtils.convert(teamRoleMapper.selectList(predicate), TeamRoleDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id) {
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }
}
